<template>
  <div>
    <!-- 索赔结算发票审核 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="4" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      selectionType="single"
      :isSelectColumn="false"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary"   @click="claimExportData">导出</el-button>
        <!-- <el-button size="small" type="primary" :disabled="signInvoices"  @click="signInvoice">签收发票</el-button> -->
        <el-button size="small" type="primary"   @click="batchIssue">PDI&首保车辆明细下发</el-button>
      </template>
    </FilterTable>
    <!-- 材料费发票签收页面 -->
    <Drawer :visible.sync="openPartSettleSubmitPage" :title="dialogTitle" width="60%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <partInvoceSubmit @close="closePartSettleSubmitPage" @save="save" :rowData="rowData" ></partInvoceSubmit>
    </Drawer>

    <!-- 服务费发票签收页面 -->
    <Drawer :visible.sync="openServiceSettleSubmitPage" :title="dialogTitle" width="60%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <serviceInvoiceSubmit @close="closeServiceSettleSubmitPage" @save="save" :rowData="rowData" ></serviceInvoiceSubmit>
    </Drawer>

    <!-- 材料费发票明细页面 -->
    <Drawer :visible.sync="openPartSettleDetailPage" :title="dialogTitle" width="60%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <partPageInfo @close="closePartSettleDetailPage" @save="save" :rowData="rowData" ></partPageInfo>
    </Drawer>

    <!-- 服务费发票明细页面 -->
    <Drawer :visible.sync="openServiceSettleDetailPage" :title="dialogTitle" width="60%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <servicePageInfo @close="closeServiceSettleDetailPage" @save="save" :rowData="rowData" ></servicePageInfo>
    </Drawer>

  </div>
</template>
<script>
import moment from "moment";  
import partInvoceSubmit from './partInvoceSubmit.vue';
import serviceInvoiceSubmit from './serviceInvoiceSubmit.vue';
import partPageInfo from './partPageInfo.vue';
import servicePageInfo from './servicePageInfo.vue';
import { exportExcelFile } from '@/utils/index';
import { getAppId } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import { getInvoiceAuditInfo ,getPartType,exporturl,auditInvoiceResult,batchIssueDetail} from '@/api/claim/factory/claimStatamentInvoiceAudit';
import { troubleDesc } from '../../../../api/repair/precontract/precontractStatus';
// mock 方法 
//import mockFun from "@/mock/fun"
//const { mockList } = mockFun.ClaimStatementQueryDealer.mockFunWrap()// 涉及闭包 所以需要先调用外层函数一次 然后通过解构获取对应mock方法

export default {
  name:"claimStatementInvoiceAudit",
  mixins: [dictionary],
  components: {
    partInvoceSubmit,
    serviceInvoiceSubmit,
    partPageInfo,
    servicePageInfo
  },
  data() {
    return {
      dataList: [],
      openPartSettleSubmitPage: false,
      openServiceSettleSubmitPage: false,
      openPartSettleDetailPage: false,
      openServiceSettleDetailPage: false,
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      fetchapi: getInvoiceAuditInfo ,//getClaimAccountInfo,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      rowData: {},
      dataType:'',
      columns: this.creatHeader(),
      fetchParams: {xhrAbort: false}, //ipa 接口
      selectedRows: [], 
      dialogTitle: '结算信息详情',
      visibleAdd: false,
      signInvoices: true,
    };
  },
  created() {
    //this.chuqi();
  },
  methods: {
    //选中
    // selectChange(rows) {
    //   this.selectedRows = rows;
    //   if(this.selectedRows[0].orderStatus === 50051002||this.selectedRows[0].orderStatus === 50051004){
    //     this.signInvoices=false;
    //   }else {
    //     this.signInvoices=true;
    //   }
    //   console.log(this.selectedRows);
    // },

    

    closePartSettleSubmitPage() {
      this.openPartSettleSubmitPage = false;
      this.fetchParams = { ...this.fetchParams };
      this.auditInvoices=true;
    },
    closeServiceSettleSubmitPage(){
        this.openServiceSettleSubmitPage = false;
        this.fetchParams = { ...this.fetchParams };
        this.auditInvoices=true;
    },
    closePartSettleDetailPage() {
      this.openPartSettleDetailPage = false;
    },
    closeServiceSettleDetailPage(){
        this.openServiceSettleDetailPage = false;
    },
   
    closeDialogPage(){
      this.openDialogPage = false;
    },
    signInvoice(val) {
        this.rowData =val;
        this.dialogTitle="索赔结算发票签收";
        if(this.rowData.finalStatementType === 50021002){// 材料费
          this.openPartSettleSubmitPage = true;
        }else if(this.rowData.finalStatementType === 50021001){// 服务费
         this.openServiceSettleSubmitPage = true;
        }
    },
    chooseOrderNo(val) {
      this.rowData = val;
      console.log("2222",this.rowData)
      this.dialogTitle="索赔结算发票明细";
      if(this.rowData.finalStatementType === 50021002){// 材料费
        this.openPartSettleDetailPage = true;
      }else if(this.rowData.finalStatementType === 50021001){// 服务费
        this.openServiceSettleDetailPage = true;
      }
    },
    //批量下发
    batchIssue(){
      this.$confirm('是否确认下发PDI&首保车辆明细?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(()=>{
          batchIssueDetail()
            .then(res => {
              if(res.resultCode == 200){
                this.$notify.success('下发成功');
              }
            })
            .catch(err => {
              this.$notify.error('下发失败');
            });
        })
        .catch(() => {
          this.$message({
              type: 'info',
              message: '已取消'
            });
        });
    },
    
    save() {
      this.visible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    // 导出
   async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000206' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginCreatedAt = this.topFilterInit.find(x => x.fieldName == "invoiceDateStart|invoiceDateEnd").initialValue[0];
      // let endCreatedAt = this.topFilterInit.find(x => x.fieldName == "invoiceDateStart|invoiceDateEnd").initialValue[1];
      // if(!beginCreatedAt) {
      //   this.$notify.warning({ title: '提示信息', message: '开票开始日期不可为空！' });
      //   return
      // }
      // if(!endCreatedAt) {
      //   this.$notify.warning({ title: '提示信息', message: '开票结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endCreatedAt).format('YYYY-MM-DD')).diff(moment(beginCreatedAt), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '开票日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endCreatedAt).format('YYYY-MM-DD')).diff(moment(beginCreatedAt), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '开票日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, {xhrAbort: false});
      // obj.invoiceDateStart = this.topFilterInit.find(x => x.fieldName == "invoiceDateStart|invoiceDateEnd").initialValue[0];
      // obj.invoiceDateEnd = this.topFilterInit.find(x => x.fieldName == "invoiceDateStart|invoiceDateEnd").initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '索赔结算单信息');
      // });
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val,{xhrAbort: false});
      console.log(val);
    },
    // 索赔结算单导出
    async claimExportData() {
      await exporturl(this.fetchParams).then(res => {
        console.log(res);
        exportExcelFile(res.data, '索赔结算发票审核导出');
      });
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '服务中心代码', fieldName: 'dealerCode', placeholder: '请输入服务站代码...', initialValue: '' },
        { type: 'INPUT', label: '服务中心简称', fieldName: 'dealerName', placeholder: '请输入服务站简称...', initialValue: '' },
        { type: 'INPUT', label: '结算单号', fieldName: 'settleNo', placeholder: '请输入结算单号...', initialValue: '' },
        { type: 'INPUT', label: '结算月份', fieldName: 'settleDate', placeholder: '请输入索赔月份...', initialValue: '' },
        { type: 'SELECT', label: '发票状态', fieldName: 'orderStatus', placeholder: '请选择发票状态...', initialValue: '', itemList: [{value:50051002,text:"待签收"},
        {value:50051003,text:"已签收"},{value:50051004,text:"财务驳回"},{value:50051005,text:"已转款"}]},
        
        // { type: 'SELECT', label: '索赔车辆属性', fieldName: 'carType', placeholder: '请选择索赔车辆属性...', initialValue: '',itemList: this.createDlrDictList('5543') }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          fixed: 'left',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          width:110,
          dataIndex: 'column-action',
          render: (props, h) => {
            return (
              <div>
              <multiuse-button size="mini" type="text" disabled={props.row.orderStatus!=50051002&&props.row.orderStatus!=50051004} onClick={() => this.signInvoice(props.row)}>
                  签收发票
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.chooseOrderNo(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },  
        { title: '服务中心代码', dataIndex: 'dealerCode', sorter: true },
        { title: '服务中心简称', dataIndex: 'dealerName', sorter: true },                       
        { title: '发票状态', dataIndex: 'orderStatus', sorter: true, showOverflowTooltip: true, dictItems: this.createDictList('5005')},
        { title: '结算单号', dataIndex: 'settleNo', sorter: true },
        { title: '结算月份', dataIndex: 'settleDate', sorter: true },
        { title: '结算单类型', dataIndex: 'finalStatementType', dictItems: this.createDictList('5002')},
        { title: '结算金额', dataIndex: 'settleAmount', sorter: true,precision: 2 },
        // { title: '结算单类型', dataIndex: 'finalStatementType', sorter: true },
       { title: '发票类别', dataIndex: 'orderType', dictItems: this.createDictList('5010')},
        { title: '结算比例', dataIndex: 'settleRate', sorter: true },
        { title: '开票金额', dataIndex: 'invoiceAmount', sorter: true ,precision: 2},
        { title: '发票张数', dataIndex: 'invoiceNum', sorter: true },
        { title: '更新时间', dataIndex: 'updateDate', sorter: true },
        
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
